package weaver.controller;

import weaver.model.WeaverModel;
import weaver.view.WeaverView;

/**
 * Controller class for the Weaver game.
 * Handles user input and game flow.
 */
public class WeaverController {
    private WeaverModel model;
    private WeaverView view;
    
    public WeaverController(WeaverModel model, WeaverView view) {
        this.model = model;
        this.view = view;
    }
    
    /**
     * Starts a new game with the given start and target words.
     * If either word is null, random words will be used.
     * 
     * @param startWord The starting word, or null for random
     * @param targetWord The target word, or null for random
     */
    public void startGame(String startWord, String targetWord) {
        assert startWord == null || startWord.length() == 4 : "Start word must be null or 4 letters long";
        assert targetWord == null || targetWord.length() == 4 : "Target word must be null or 4 letters long";
        try {
            // If random words is enabled, use random words
            if (model.isRandomWords()) {
                startWord = model.getRandomWord();
                do {
                    targetWord = model.getRandomWord();
                } while (startWord.equals(targetWord));
            } else {
                // Use default words if not provided
                if (startWord == null || targetWord == null || startWord.isEmpty() || targetWord.isEmpty()) {
                    startWord = "COLD";
                    targetWord = "WARM";
                }
            }
            
            // Validate word length
            if (startWord.length() != 4 || targetWord.length() != 4) {
                if (model.isValidating()) {
                    view.showError("Words must be exactly 4 letters long");
                }
                return;
            }
            
            // 先清空当前路径
            model.resetGame();
            
            // 设置新单词并立即通知观察者
            model.setWords(startWord, targetWord);
        } catch (Exception e) {
            if (model.isValidating()) {
                view.showError("Error starting game: " + e.getMessage());
            }
        }
    }
    
    /**
     * Handles user input for the game.
     * 
     * @param input The word entered by the user
     */
    public void handleInput(String input) {
        try {
            input = input.toLowerCase();
            
            if (input.length() != 4) {
                if (model.isValidating()) {
                    view.showError("Word must be exactly 4 letters long");
                }
                return;
            }
            
            if (!model.isValidWord(input)) {
                if (model.isValidating()) {
                    view.showError("Not a valid word");
                }
                return;
            }
            
            if (!model.isValidMove(input)) {
                if (model.isValidating()) {
                    view.showError("Invalid move - must change exactly one letter");
                }
                return;
            }
            
            model.makeMove(input);
        } catch (Exception e) {
            if (model.isValidating()) {
                view.showError("Error processing input: " + e.getMessage());
            }
        }
    }
    
    /**
     * Resets the game to its initial state.
     */
    public void resetGame() {
        try {
            model.resetGame();
        } catch (Exception e) {
            if (model.isValidating()) {
                view.showError("Error resetting game: " + e.getMessage());
            }
        }
    }
    
    /**
     * Sets the game flags.
     * 
     * @param showError Whether to show error messages
     * @param showPath Whether to show the solution path
     * @param randomWords Whether to use random words
     */
    public void setFlags(boolean showError, boolean showPath, boolean randomWords) {
        model.setFlags(showError, showPath, randomWords);
    }
}